package com.om39a.plm.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.apache.log4j.Logger;

import com.om39a.plm.exceptions.PageLoadFailedException;
import com.om39a.plm.service.PLMService;
import com.om39a.plm.utils.PLMConstants;

/**
 * @author om39a
 * 
 */
public class PLMCrawler {

	private static Logger logger = Logger.getLogger(PLMCrawler.class);

	private JFrame plmFrame;
	private JTextField txtUserName;
	private JTextField textPassword;
	private JTextField textForumUrl;
	private JTextField txtMinReplies;
	private JTextField txtThreadUrlInputFile;
	private JTextField txtUserUrlInputFile;
	private JTextField txtUserUrlOutputFile;
	private JTextField txtThreadUrlOutputFile;
	private JTextField textEndPageURL;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		/*
		 * System.setProperty("log4j.configuration",
		 * "classpath:log4j.properties");
		 * 
		 * System.out.println(System.getProperty("log4j.configuration"));
		 */

		logger.info("=================");
		logger.info("Starting PLM Crawler");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PLMCrawler window = new PLMCrawler();
					window.plmFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
					logger.info(e);
				}
			}
		});
		logger.info("=================");
	}

	/**
	 * Create the application.
	 */
	public PLMCrawler() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		plmFrame = new JFrame();
		JPanel panel_5 = new JPanel();
		panel_5.setBounds(444, 98, 230, 414);
		plmFrame.getContentPane().add(panel_5);
		panel_5.setLayout(null);

		final JTextPane textPane1 = new JTextPane();
		textPane1.setEditable(false);
		textPane1
				.setText("1. Login\r\n2. Enter forum base url (e.g /forum/hiv/topics)\r\n3. Min number of replies default to 10\r\n4. Thread output file name. If not given step1.txt file will be generated .\r\n5. On clicking on Crawl, it will collect all thread urls in the base forum and write it to given file name or step1.txt\r\n6. Thread ur;'s input file will be taken as step1.txt if not given. User Url's output file will be step2.txt by default. \r\n7. On click of copy thread posts, a template1.xls file will be generated with thread post contents. A user probile url list will be geenrated in step2.txt or on given file name.\r\n8.  User Url's input file will be step2.txt by default or User Url's output file as input to this step.\r\n9. On click of copy profile data, template2.xls and template3.xls will be generated with the user data and conditions data.\r\n\r\n\r\n");
		textPane1.setBounds(10, 0, 210, 403);
		panel_5.add(textPane1);

		plmFrame.setBounds(100, 100, 700, 561);
		plmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		plmFrame.getContentPane().setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 664, 24);
		plmFrame.getContentPane().add(panel);

		JLabel lblPlmCrawler = new JLabel("PLM Crawler");
		panel.add(lblPlmCrawler);

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 46, 664, 38);
		plmFrame.getContentPane().add(panel_1);
		panel_1.setLayout(null);

		final JLabel lblStatusMessage = new JLabel("");
		lblStatusMessage.setBounds(531, 11, 123, 14);
		panel_1.add(lblStatusMessage);

		JLabel lblUserName = new JLabel("User Name");
		lblUserName.setBounds(10, 11, 86, 14);
		panel_1.add(lblUserName);

		txtUserName = new JTextField();
		txtUserName.setBounds(86, 5, 86, 20);
		panel_1.add(txtUserName);
		txtUserName.setColumns(10);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(196, 11, 83, 14);
		panel_1.add(lblPassword);

		textPassword = new JTextField();
		textPassword.setBounds(289, 8, 86, 20);
		panel_1.add(textPassword);
		textPassword.setColumns(10);

		final JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Map<String, String> loginArgs = new HashMap<String, String>();
				loginArgs.put(PLMConstants.USER_NAME, txtUserName.getText());
				loginArgs.put(PLMConstants.PASSWORD, textPassword.getText());
				System.out.println(loginArgs);
				if (PLMService.getInstance().login(loginArgs)) {
					lblStatusMessage.setText("Login Success");
				} else {
					lblStatusMessage.setText("Login Failed");
				}
			}
		});
		btnLogin.setBounds(417, 7, 89, 23);
		panel_1.add(btnLogin);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 95, 424, 152);
		plmFrame.getContentPane().add(panel_2);
		panel_2.setLayout(null);

		JLabel lblForumUrl = new JLabel("Forum URL");
		lblForumUrl.setBounds(10, 11, 124, 14);
		panel_2.add(lblForumUrl);

		textForumUrl = new JTextField();
		textForumUrl.setBounds(145, 8, 236, 20);
		panel_2.add(textForumUrl);
		textForumUrl.setColumns(10);

		JLabel lblMinReplies = new JLabel("Min Replies");
		lblMinReplies.setBounds(10, 91, 124, 14);
		panel_2.add(lblMinReplies);

		txtMinReplies = new JTextField();
		txtMinReplies.setText("10");
		txtMinReplies.setBounds(142, 88, 86, 20);
		panel_2.add(txtMinReplies);
		txtMinReplies.setColumns(10);

		final JButton btnCrawlThreadUrls = new JButton("Crawl");
		btnCrawlThreadUrls.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Map<String, String> forumUrlArgs = new HashMap<String, String>();
				forumUrlArgs.put(PLMConstants.FORUM_URL, textForumUrl.getText());
				forumUrlArgs.put(PLMConstants.MIN_REPLIES,
						txtMinReplies.getText());
				forumUrlArgs.put(PLMConstants.THREAD_URL_OUTPUT_FILE,
						txtThreadUrlOutputFile.getText());
				forumUrlArgs.put(PLMConstants.END_PAGE_URL,
						textEndPageURL.getText());
				try {
					PLMService.getInstance().grabThreadUrls(forumUrlArgs);
				} catch (PageLoadFailedException e1) {
					e1.printStackTrace();
					logger.info(e1);
				}
				btnCrawlThreadUrls.setText("Crawling in progress");
			}
		});

		JLabel lblThreadUrlsOutput = new JLabel("Output File");
		lblThreadUrlsOutput.setBounds(238, 90, 59, 17);
		panel_2.add(lblThreadUrlsOutput);

		txtThreadUrlOutputFile = new JTextField();
		txtThreadUrlOutputFile.setColumns(10);
		txtThreadUrlOutputFile.setBounds(307, 88, 100, 20);
		panel_2.add(txtThreadUrlOutputFile);
		btnCrawlThreadUrls.setBounds(10, 118, 236, 23);
		panel_2.add(btnCrawlThreadUrls);

		JLabel lblForumEndUrl = new JLabel("Forum End URL");
		lblForumEndUrl.setBounds(10, 45, 124, 14);
		panel_2.add(lblForumEndUrl);

		textEndPageURL = new JTextField();
		textEndPageURL.setColumns(10);
		textEndPageURL.setBounds(145, 42, 236, 20);
		panel_2.add(textEndPageURL);

		JPanel panel_3 = new JPanel();
		panel_3.setBounds(10, 258, 424, 145);
		plmFrame.getContentPane().add(panel_3);
		panel_3.setLayout(null);

		JLabel lblThreadUrlFile = new JLabel("Thread Url's Input File");
		lblThreadUrlFile.setBounds(10, 11, 146, 17);
		panel_3.add(lblThreadUrlFile);

		txtThreadUrlInputFile = new JTextField();
		txtThreadUrlInputFile.setBounds(173, 8, 229, 20);
		panel_3.add(txtThreadUrlInputFile);
		txtThreadUrlInputFile.setColumns(10);

		JLabel lblOutputFile = new JLabel("User Url's Output File");
		lblOutputFile.setBounds(10, 53, 146, 17);
		panel_3.add(lblOutputFile);

		txtUserUrlOutputFile = new JTextField();
		txtUserUrlOutputFile.setColumns(10);
		txtUserUrlOutputFile.setBounds(173, 50, 229, 20);
		panel_3.add(txtUserUrlOutputFile);

		final JButton btnCopyThreadPosts = new JButton("Copy Thread Posts");
		btnCopyThreadPosts.setBounds(10, 100, 237, 23);
		panel_3.add(btnCopyThreadPosts);

		btnCopyThreadPosts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Map<String, String> threadPostsUrlArgs = new HashMap<String, String>();
				threadPostsUrlArgs.put(PLMConstants.THREAD_URL_INPUT_FILE,
						txtThreadUrlInputFile.getText());
				threadPostsUrlArgs.put(PLMConstants.USER_URL_OUTPUT_FILE,
						txtUserUrlOutputFile.getText());
				PLMService.getInstance().grabThreadPosts(threadPostsUrlArgs);
				System.out.println(threadPostsUrlArgs);
				btnCopyThreadPosts.setText("Thread posts copy in progress");
			}
		});

		JPanel panel_4 = new JPanel();
		panel_4.setBounds(10, 414, 424, 120);
		plmFrame.getContentPane().add(panel_4);
		panel_4.setLayout(null);

		JLabel lblUserUrlInput = new JLabel("User Url's Input");
		lblUserUrlInput.setBounds(10, 38, 154, 17);
		panel_4.add(lblUserUrlInput);

		txtUserUrlInputFile = new JTextField();
		txtUserUrlInputFile.setColumns(10);
		txtUserUrlInputFile.setBounds(177, 36, 226, 20);
		panel_4.add(txtUserUrlInputFile);

		final JButton btnCopyProfileData = new JButton("Copy Profile Data");
		btnCopyProfileData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Map<String, String> userProfileUrlArgs = new HashMap<String, String>();
				userProfileUrlArgs.put(PLMConstants.USER_DATA_URL_INPUT_FILE,
						txtUserUrlInputFile.getText());
				System.out.println(userProfileUrlArgs);

				PLMService.getInstance().grabUserData(userProfileUrlArgs);
			}
		});
		btnCopyProfileData.setBounds(10, 86, 235, 23);
		panel_4.add(btnCopyProfileData);
	}
}
